package com.tencent.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;

import com.tencent.common.Configure;

public class AppSignUtil {
    
    /**
     * 生成签名
     */
    public static String genAppSign(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        ArrayList<String> list = new ArrayList<String>();
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(entry.getValue()!=""){
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        //Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        for(int i = 0; i < size; i ++) {
            sb.append(arrayToSort[i]);
        }
        sb.append("key=");
        sb.append(Configure.getKey());
        String appSign = MD5_4Android.getMessageDigest(sb.toString().getBytes()).toUpperCase();
        //String appSign = MD5(sb).toUpperCase();
        return appSign;
      
      /*  
        for (int i = 0; i < params.size(); i++) {
            sb.append(params.get(i).getName());
            sb.append('=');
            sb.append(params.get(i).getValue());
            sb.append('&');
        }
        sb.append("key=");
        sb.append(Configure.getKey());

        String appSign = MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();
        return appSign;*/
    }

    /**
     * 生成签名
     */
    public static String genAppSign(List<NameValuePair> params) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < params.size(); i++) {
            sb.append(params.get(i).getName());
            sb.append('=');
            sb.append(params.get(i).getValue());
            sb.append('&');
        }
        sb.append("key=");
        sb.append(Configure.getKey());

        String appSign = MD5_4Android.getMessageDigest(sb.toString().getBytes()).toUpperCase();
        return appSign;
    }


}
